package controllers.manage.coach;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.AliyunOssUploader;
import helper.GlobalConfig;
import jodd.util.StringUtil;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.venue.coach.Coach;
import models.venue.fitness.Fitnesses;
import play.Logger;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教练管理
 * Created by hanbin on 17/4/6.
 */
@With(BaseController.class)
public class CoachCtrl extends BaseController {

    /**
     * 跳转到教练管理页面
     */
    public static void index(Integer pageNumber, String coachName){

        Merchant merchant = Secure.getLoginUser().merchant;

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();

        if(StringUtil.isNotBlank(coachName)) {
            searchMap.put("coachName", "%"+coachName+"%");
        }

        if(merchant != null){
            searchMap.put("merchantId", merchant.id);
        }

        flash.put("menuId", "9");

        JPAExtPaginator<Coach> resultPage = Coach.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, coachName);
    }

    /**
     * 跳转到新增教练页面
     */
    public static void add(){
        Merchant merchant = Secure.getLoginUser().merchant;
        List<Fitnesses> fitnesses = Fitnesses.findAllFitnessesByMerchant(merchant.id);
        render(fitnesses);
    }

    /**
     * 跳转到修改教练页面
     */
    public static void modify(long id, Integer pageNumber){
        Merchant merchant = Secure.getLoginUser().merchant;
        List<Fitnesses> fitnesses = Fitnesses.findAllFitnessesByMerchant(merchant.id);
        Coach coach = Coach.findById(id);
        render(coach, pageNumber, fitnesses);
    }


    /**
     * 创建教练
     */
    public static void create(Coach coach, File coachImage){

        Merchant merchant = Secure.getLoginUser().merchant;
        coach.merchant = merchant;

        try {
            if(coachImage != null) {
                BufferedImage sourceImg = ImageIO.read(new FileInputStream(coachImage));
                Integer width = sourceImg.getWidth();
                Integer height = sourceImg.getHeight();
                if(((double)width/(double)height) != ((double)4/(double)3)){
                    coach.deleted = DeletedStatus.UN_DELETED;
                    coach.save();
                    flash.put("msg" , "图片比例不是4:3");
                    modify(coach.id, 1);
                }
                coach.coachImage = AliyunOssUploader.uploadPublicFile(coachImage , 300);
            }
            coach.deleted = DeletedStatus.UN_DELETED;
            coach.save();
            flash.put("msg" , "教练创建成功");
            redirect("/BS0014/admin/coach/index/1");
        } catch (Exception e) {
            Logger.warn(e, "LOG01750: coach create failed!");
        }
    }

    /**
     * 修改教练
     */
    public static void update(Long id, Coach coach, Integer pageNumber, File coachImage){
        try {
            if(coachImage != null) {
                BufferedImage sourceImg = ImageIO.read(new FileInputStream(coachImage));
                Integer width = sourceImg.getWidth();
                Integer height = sourceImg.getHeight();
                if(((double)width/(double)height) != ((double)4/(double)3)){
                    Coach.update(id , coach);
                    flash.put("msg" , "图片比例不是4:3");
                    modify(id, pageNumber);
                }
                coach.coachImage = AliyunOssUploader.uploadPublicFile(coachImage , 300);
            }
            Coach.update(id , coach);
            flash.put("msg" , "教练修改成功");
            redirect("/BS0014/admin/coach/index/"+pageNumber);
        } catch (Exception e) {
            Logger.warn(e, "LOG01750: coach update failed!");
        }
    }

    /**
     * 删除教练
     */
    public static void delete(long id , Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Coach coach = Coach.findById(id);
        coach.deleted=DeletedStatus.DELETED;
        coach.save();
        flash.put("msg" , "教练删除成功");
        redirect("/BS0014/admin/coach/index/"+pageNumber);
    }
}
